extends MarginContainer

const PORT: int = 3000


@onready var option_button: OptionButton = $VBoxContainer/VBoxContainer2/OptionButton
@onready var option_diff_button: OptionButton = $VBoxContainer/VBoxContainer3/OptionButton

@onready var begin_button: Button = %BeginButton
@onready var back_button: Button = %BackButton


@onready var error_container: MarginContainer = $ErrorContainer
@onready var client_error_label: Label = %ClientErrorLabel
@onready var server_error_label: Label = %ServerErrorLabel
@onready var error_confirm_button: Button = %ErrorConfirmButton



@onready var main_menu_scene: PackedScene = load("res://ui/main_menu/main_menu.tscn")

var main_scene: PackedScene = preload("res://main.tscn")


var is_connecting: bool

func _ready():
	error_container.visible = false
	error_confirm_button.pressed.connect(_on_error_confirm_pressed)

	option_button.text = MultiplayerConfig.display_type

	validate()
	
	UIAudioManager.register_buttons([
		back_button,
		begin_button,
		error_confirm_button
	])


func validate():
	
	MultiplayerConfig.display_type = option_button.text
	MultiplayerConfig.display_type2 = MultiplayerConfig.type2.get(option_button.selected)

	var is_valid_type := MultiplayerConfig.type.has(MultiplayerConfig.display_type)
	
	MultiplayerConfig.diff_type = option_diff_button.text
	
	var is_valid_diff_type := MultiplayerConfig.diff.has(MultiplayerConfig.diff_type)
	
	
	begin_button.disabled = !is_valid_type || !is_valid_diff_type




func _on_back_pressed():
	get_tree().change_scene_to_packed(main_menu_scene)


func _on_text_changed():
	validate()


func _on_error_confirm_pressed():
	error_container.visible = false



func _on_option_button_item_selected(index: int) -> void:
	MultiplayerConfig.display_type = option_button.text
	MultiplayerConfig.display_type2 = MultiplayerConfig.type2.get(option_button.selected)

	
func _on_option_button_item_diff_selected(index: int) -> void:
	MultiplayerConfig.diff_type = option_diff_button.text


func _on_begin_button_pressed() -> void:
	get_tree().change_scene_to_packed(main_scene)


func _on_back_button_pressed() -> void:
	get_tree().change_scene_to_packed(main_menu_scene)
